import { APP_NAME, DEVICE_ID, USER_ID } from '../constants';
import { get, post, put } from '../utils/http';
import { getStorageItem } from '@/utils/storage';

export const paymentAPI = {
  // 获取权益剩余次数
  getAvailableTimes: async () => {
    try {
      const res = await get<Payment.PaymentResponse>(`/member-service/user/credit/get`);
      return res.data;
    } catch (error) {
      console.error(error);
      throw new Error('getAvailableTimes failed');
    }
  },
  // 创建支付订单
  create_payment_order: async (data: Payment.PaymentParams) => {
    try {
      const res = await post<Payment.PaymentResult>('/payment-service/payment/orders', data, {
        headers: {
          'ts-app-name': APP_NAME,
        },
      }).catch((error) => {
        throw new Error(error.data.msg);
      });
      return res.data;
    } catch (error) {
      console.error(error);
      throw new Error('create_payment_order failed');
    }
  },
  create_payment_order_android: async (data: Payment.PaymentParams) => {
    try {
      const res = await post<Payment.PaymentResult>('/turing-payment-google/payment/orders', data, {
        headers: {
          'ts-app-name': APP_NAME,
        },
      }).catch((error) => {
        throw new Error(error.data.msg);
      });
      return res.data;
    } catch (error) {
      console.error(error);
      throw new Error('create_payment_order failed');
    }
  },
  // 根据订单ID修改订单为支付中 (苹果支付不需要这一步）
  change_payment_status_as_paying: async (data: Payment.PaymentOrderID) => {
    try {
      await put(`/payment-service/payment/orders/${data}`);
    } catch (error) {
      console.error(error);
      throw new Error('change_payment_status_as_paying failed');
    }
  },
  // 根据交易id查询应用内交易查询
  getTransactionsInfo: async (transactionId: string, orderId: number) => {
    try {
      const res = await get<Payment.TransactionTrueOrFalse>(
        `/payment-service/apple-pay/v2/transactions/${transactionId}/${orderId}`,
        undefined,
        {
          'ts-app-name': APP_NAME,
        },
      );
      return res.data;
    } catch (error) {
      console.error(error);
      throw new Error('getTransactionsInfo failed');
    }
  },
  /** 恢复订阅 */
  restoreSubscription: async (receipt: string) => {
    try {
      return await post(
        `/payment-service/apple-pay/v2/app/changeEquipment`,
        {
          receipt: receipt,
          equipmentNum: getStorageItem(DEVICE_ID),
        },
        {
          headers: {
            'ts-app-name': APP_NAME,
            'ts-user-id': getStorageItem(USER_ID) || '',
          },
        },
      );
    } catch (error) {
      console.log(error);
      throw new Error('恢复订阅失败');
    }
  },
  //   根据内购产品code获取产品信息
  getProductDetailList: async (params: Payment.ProductListRequest) => {
    try {
      const res = await get<Payment.ProductListResponse>('/turing-paywall/product/codes', {
        ...params,
      });
      return res.data;
    } catch (error) {
      console.error(error);
      throw new Error('getProductDetailList failed');
    }
  },
  // 获取支付墙详情
  getPaywallDetail: async (params: Payment.PaywallRequest) => {
    try {
      const res = await get<Payment.PaywallResponse>('/turing-paywall/paywall/getPaywall', {
        ...params,
      });
      return res.data;
    } catch (error) {
      console.error(error);
      throw new Error('getPaywallDetail failed');
    }
  },
};
